const db = require('../db/db')
const {paging} = require('../utils/paging')
module.exports = {
    // 获取商品
    GETGOODS: async (ctx, next) => {
        const {startPage,endPage} = await paging(ctx)
        const {belongToShop} = ctx.request.query
        try {
            let [res] = await db.promise().query('select id,goodsName,goodsItn,goodsPrice,goodsImg,goodsNum from goods where belongToShop=? limit ?,?',
            [belongToShop,startPage,endPage])
            let [total] = await db.promise().query('select * from goods')
            ctx.body ={
                code:200,
                msg:'获取成功',
                goodsData:res,
                total:total.length
            }
        } catch (error) {
            ctx.body ={
                code:500,
                msg:'获取失败'+error
            }
        }
        
    },
    // 添加商品
    ADDGOODS: async (ctx, next) => {
        const {id,goodsName,goodsItn,goodsPrice,goodsImg,goodsNum,belongToShop } = ctx.request.body
        try {
            let [res] = await db.promise().query('insert into goods(id,goodsName,goodsItn,goodsPrice,goodsImg,goodsNum,belongToShop) values(?,?,?,?,?,?,?)',
            [id,goodsName,goodsItn,goodsPrice,goodsImg,goodsNum,belongToShop ])
            if(res.affectedRows > 0 ){
                ctx.body ={
                    code:200,
                    msg:'添加成功',
                }            }
        } catch (error) {
            ctx.body ={
                code:500,
                msg:'添加失败'+error,
            }
        }
        
        
    },
    // 修改商品
    UPDATEGOODS: async (ctx, next) => {
        const {id,goodsName,goodsItn,goodsPrice,goodsImg,goodsNum } = ctx.request.body
        try {
            let [res] =await db.promise().query('update goods set goodsName=?,goodsItn=?,goodsPrice=?,goodsImg=?,goodsNum=? where id=?',
            [goodsName,goodsItn,goodsPrice,goodsImg,goodsNum,id])
            if(res.affectedRows > 0 ){
                ctx.body ={
                    code:200,
                    msg:'修改成功',
                }            }
        } catch (error) {
            ctx.body ={
                code:500,
                msg:'修改失败'+error,
            }
        }
        
        
    },
    // 删除商品
    DELGOODS: async (ctx, next) => {
        const {id} = ctx.request.body
        try {
            let [res] = await db.promise().query('delete from goods where id=?',id)
            if(res.affectedRows > 0 ){
                ctx.body ={
                    code:200,
                    msg:'删除成功',
                }           
             }
        } catch (error) {
            ctx.body ={
                code:500,
                msg:'删除失败'+error,
            }
        }
       
    },
    // 搜索商品
    SEARCHGOODS: async (ctx, next) => {
        const {keyWord,belongToShop} = ctx.request.body
        try {
            let [res] = await db.promise().query(`select id,goodsName,goodsItn,goodsPrice,goodsImg ,goodsNum from goods where goodsName like "%${keyWord}%"and belongToShop=?` 
            ,[belongToShop])
           if(res.length > 0){
            ctx.body ={
                code:200,
                msg:'搜索商品成功',
                goodsList:res,
                total:res.length
            }
           }else{
            ctx.body ={
                code:200,
                msg:'没有该商品',
            }
           }
        } catch (error) {
            ctx.body ={
                code:500,
                msg:'搜索商品失败'+error,
            }
        }
    },
}